package com.tangyuewei.datasource.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.tangyuewei.datasource.dao.UserMapper;
import com.tangyuewei.datasource.domain.User;
import com.tangyuewei.datasource.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author Webster
 * <p>
 * Description:
 * </p>
 * @date 2021/7/12
 * @see com.tangyuewei.datasource.service.impl
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    @DS("slave1")
    @DSTransactional
    public int deleteByPrimaryKey(Integer id) {
        return userMapper.deleteByPrimaryKey(id);
    }

    @Override
    @DS("master")
    public User selectByMaster(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    @DS("slave1")
    public User selectBySlave1(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    @DS("slave2")
    public User selectBySlave2(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    @DSTransactional
    public void batchInsert(User record) {
        insertMaster(record);
        insertSlave1(record);
        insertSlave2(record);
    }

    @Override
    @DS("master")
    @DSTransactional
    public int insertMaster(User record) {
        return userMapper.insertSelective(record);
    }

    @Override
    @DS("slave1")
    @DSTransactional
    public int insertSlave1(User record) {
        return userMapper.insertSelective(record);
    }

    @Override
    @DS("slave2")
    @DSTransactional
    public int insertSlave2(User record) {
        return userMapper.insertSelective(record);
    }

    @Override
    public int updateByPrimaryKeySelective(User record) {
        return userMapper.updateByPrimaryKeySelective(record);
    }

}
